// Copyright 2020 newtbig Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dispatcher

import (
	"fmt"
	"path/filepath"

	"gitee.com/lakertt/newtbig/common"
	"gitee.com/lakertt/newtbig/module"
	"gitee.com/lakertt/newtbig/msg/framepb"
)

type proxy struct {
	msgKey    string
	bcKey     string
	key       string
	serType   string
	serName   string
	appName   string
	ver       string
	app       module.App
	serStatus string
}

func (p *proxy) init(serType string, key string, app module.App, status string) {
	p.app = app
	p.key = key
	p.appName = app.Options().AppName
	p.ver = app.Options().Version
	p.serType = filepath.Base(serType)
	p.serName = filepath.Base(p.key)
	p.msgKey = fmt.Sprintf("%s.%s.%s", p.appName, p.serType, p.serName)
	p.bcKey = fmt.Sprintf("%s.%s.%s.%s", p.appName, p.serType, p.serName, common.BROATCAST)
	p.serStatus = status
}

func (p *proxy) OnClose() error {
	return nil
}

func (p *proxy) GetKey() string {
	return p.key
}

func (p *proxy) GetMsgKey() string {
	return p.msgKey
}

func (p *proxy) GetBCKey() string {
	return p.bcKey
}

func (p *proxy) forward(msg *framepb.Msg) error {
	err := p.app.GetRPC().CallNR(msg)
	return err
}

func (p *proxy) update(status string) error {
	p.serStatus = status
	return nil
}
